package com.hina.rec.application.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hina.rec.application.convert.WorkflowConvert;
import com.hina.rec.application.convert.WorkflowDataConvert;
import com.hina.rec.application.service.WorkflowService;
import com.hina.rec.domain.mapper.WorkflowDataMapper;
import com.hina.rec.domain.mapper.WorkflowMapper;
import com.hina.rec.domain.model.WorkflowDataDO;
import com.hina.rec.domain.model.WorkflowDO;
import com.hina.rec.endpoint.req.WorkflowDataSearchReq;
import com.hina.rec.endpoint.req.WorkflowSearchReq;
import com.hina.rec.endpoint.res.WorkflowDataRes;
import com.hina.rec.endpoint.res.WorkflowRes;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 工作流服务实现类
 *
 * @author dequal
 * @since 2025-05-23
 */
@Service
@RequiredArgsConstructor
public class WorkflowServiceImpl implements WorkflowService {

    private final WorkflowMapper workflowMapper;
    private final WorkflowDataMapper workflowDataMapper;

    @Override
    public Page<WorkflowRes> page(WorkflowSearchReq req) {
        // 构建查询条件
        LambdaQueryWrapper<WorkflowDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(req.getType()), WorkflowDO::getType, req.getType())
                .like(StringUtils.isNotBlank(req.getTitle()), WorkflowDO::getTitle, req.getTitle())
                .eq(StringUtils.isNotBlank(req.getStatus()), WorkflowDO::getStatus, req.getStatus())
                .orderByDesc(WorkflowDO::getCreatedAt);

        // 执行分页查询
        Page<WorkflowDO> page = workflowMapper.selectPage(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        
        // 转换结果
        Page<WorkflowRes> result = new Page<>();
        result.setRecords(page.getRecords().stream().map(WorkflowConvert.INSTANCE::toRes).toList());
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setSize(page.getSize());
        
        return result;
    }

    @Override
    public Page<WorkflowDataRes> pageData(WorkflowDataSearchReq req) {
        // 构建查询条件
        LambdaQueryWrapper<WorkflowDataDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(req.getWorkflowId() != null, WorkflowDataDO::getWorkflowId, req.getWorkflowId())
                .like(StringUtils.isNotBlank(req.getDataKey()), WorkflowDataDO::getDataKey, req.getDataKey())
                .like(StringUtils.isNotBlank(req.getDataValue()), WorkflowDataDO::getDataValue, req.getDataValue())
                .orderByDesc(WorkflowDataDO::getCreatedAt);

        // 执行分页查询
        Page<WorkflowDataDO> page = workflowDataMapper.selectPage(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        
        // 转换结果
        Page<WorkflowDataRes> result = new Page<>();
        result.setRecords(page.getRecords().stream().map(WorkflowDataConvert.INSTANCE::toRes).toList());
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setSize(page.getSize());
        
        return result;
    }
} 